﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cecbData;
 

namespace cecbClasses.Data_Adapters
{
    public class Func_Activities
    {
        public Func_Activities() 
        {
            dataContext = new DSSEntities();
        }

       DSSEntities dataContext;

        public Boolean insertIntoActivities(Data_Activities arg)
        {
            Boolean isInserted = false;

            try
            {
                Activities act = new Activities();
                act.actv_name = arg.actv_name;
                act.actv_phase = arg.Actv_phase;
                act.actv_reference = arg.Actv_reference;
                act.actv_status = arg.Actv_status;

                dataContext.AddToActivities(act);
                dataContext.SaveChanges();

                isInserted = true;
            }
            catch (Exception)
            {

                throw;
            }

            return isInserted;
        }

        public List<Activities> getAllActivities()
        {

            IQueryable<Activities> Query =
                  from c in dataContext.Activities
                  select c;

            List<Activities> Activities = Query.ToList();

            return Activities;
        }

        public List<Activities> getActivityFromProjectActivity(String prefe, String actName)
        {
            //var innerJoinQuery =
            //from c in cecbContext.ProjectImpacts
            //join d in cecbContext.Impacts on c.impt_reference equals d.impt_reference
            //where c.proj_reference.Contains(prefe) && d.impt_name.Contains(impcName)
            //select new { CustomerName = d.impt_name };
            List<Activities> SelectedImpacts = null;
            try
            {
                //String xim = dataContext.Activities.First(i => i.actv_name.Contains(actName)).actv_reference;
                IQueryable<Activities> query = from c in dataContext.ProjectActivities
                                               join b in dataContext.Activities on c.actv_reference equals b.actv_reference
                                             //  join d in dataContext.LeopoldMatrixValues on b.actv_reference equals d.actv_reference
                                               where c.proj_reference == prefe && b.actv_name.Contains(actName) 
                                               //&& d.mtrx_magnitude == null && d.mtrx_importance == null

                                               select b;

                



                SelectedImpacts = query.ToList();
            }
            catch (Exception)
            {


            }
            return SelectedImpacts;
        }


        public List<Activities> getActivitiesByName(String name)
        {
            IQueryable<Activities> Query =
                  from c in dataContext.Activities
                  where c.actv_name.Contains(name)
                  select c;

            List<Activities> SelectedActivities = Query.ToList();

            return SelectedActivities;
        }

        public List<Activities> getActivityDetailsByName(String name)
        {

            IQueryable<Activities> Query =
                  from c in dataContext.Activities
                  where c.actv_name.Contains(name)
                  select c;

            List<Activities> SelectedComponent = Query.ToList();

            return SelectedComponent;

        }

        private String getActivityReference()
        {
            int sub;


            try
            {
                var idKey = (from c in dataContext.Activities
                             orderby c.actv_id descending
                             select c).First();


                String id = idKey.actv_reference.ToString();

                sub = int.Parse(id.Substring(4)) + 1;
            }
            catch (Exception)
            {
                sub = 1;
            }
            String newId = "ACTV" + sub;
            return newId.ToString();

        }

        public bool InsertActivity(List<String> phase, List<String> act, int count, String proRef)
        {
            int itemNo = 0;
            bool IsInserted = false;
            Activities actvity;

            try
            {
                while (count > 0)
                {
                    String reference = getActivityReference();
                    actvity = new Activities();
                    actvity.actv_reference = reference;
                    actvity.actv_phase = phase[itemNo];
                    actvity.actv_name = act[itemNo];
                    actvity.actv_status = true;
                    itemNo++;
                    count--;
                    dataContext.AddToActivities(actvity);
                    dataContext.SaveChanges();

                    ProjectActivities pact = new ProjectActivities();
                    pact.proj_reference = proRef;
                    pact.actv_reference = reference;
                    pact.actv_addedBy = "";
                    pact.actv_approvedBy = "";
                    pact.proActiv_status = true;
                    dataContext.AddToProjectActivities(pact);
                    dataContext.SaveChanges();


                }


                IsInserted = true;
            }
            catch (Exception)
            {

                IsInserted = false;
            }


            return IsInserted;
        }


        public bool updateProjectActivity(String pid, String actid, String ps, String Act)
        {
            bool IsUpdated = false;
            try
            {
                dataContext.ExecuteStoreCommand("  delete from leopoldMatrixValues where proj_reference = '" + pid + "' and actv_reference = '" + actid + "' ");
                dataContext.ExecuteStoreCommand("  delete from simpleMatrixValues where proj_reference = '" + pid + "' and actv_reference = '" + actid + "' ");
                dataContext.ExecuteStoreCommand("delete from ProjectActivities where proj_reference = '" + pid + "' and actv_reference = '" + actid + "'");

                List<String> phase = new List<string>();
                List<String> acti = new List<string>();
                phase.Add(ps);
                acti.Add(Act);

                 InsertActivity(phase, acti, 1, pid);



                IsUpdated = true;
            }
            catch (Exception)
            {

                IsUpdated = false;
            }

            return IsUpdated;

        }

        public bool deleteProjectActivity(String pid, String actid)
        {
            bool IsUpdated = false;
            try
            {
                dataContext.ExecuteStoreCommand("delete from simpleMatrixValues where proj_reference = '" + pid + "' and actv_reference = '" + actid + "' ");
                dataContext.ExecuteStoreCommand("delete from leopoldMatrixValues where proj_reference = '" + pid + "' and actv_reference = '" + actid + "' ");
                dataContext.ExecuteStoreCommand("delete from ProjectActivities where proj_reference = '" + pid + "' and actv_reference = '" + actid + "'");




                IsUpdated = true;
            }
            catch (Exception)
            {

                IsUpdated = false;
            }

            return IsUpdated;

        }


        public String getActivityNameById(String id)
        {

            var idKey = (from c in dataContext.Activities
                         where c.actv_reference == id
                         select c).First();


            return idKey.actv_name.ToString();

        }


        public bool updateActivity(string activity_ref, string activity_name, string activity_phase)
        {
            bool isupdated = false;

            try
            {

                Activities activityObject = dataContext.Activities.First(i => i.actv_reference == activity_ref);

                //    ProjectImpacts projectimpactObject = dataContext.ProjectImpacts.First(i => i.ImpactsReference == impt_ref);

                activityObject.actv_name = activity_name;
            //    activityObject.actv_phase = activity_phase;


                dataContext.SaveChanges();
                isupdated = true;


            }
            catch (Exception)
            {

            }

            return isupdated;

        }

    }
}
